import chardet as chardet
import pandas as pd
import numpy as np
import os
import pickle
# -------------------
X3=None
l=0
z=0
Y3=None
for i in os.listdir("C:/Users/hs/Desktop/城市空气质量智能化评估分析系统/huang/data/city"):
    path="C:/Users/hs/Desktop/城市空气质量智能化评估分析系统/huang/data/city"+i
    f = open(path, 'rb')
    d = chardet.detect(f.read())['encoding']
    f.close()
    print(d)
    print(path)
    X=pd.read_csv(path,encoding=d)
    X=pd.DataFrame(X)
    k=1
    for j in X['type']:
        X1=X[k-1:k+13][X.columns[4:375]]
        X1=X1.to_numpy()
        k+=17
        print(X1.shape)
        if(X1.shape[0]<=13):
            break;
        if X3 is None:
            X3 = X1
            X3 = np.asarray(X1)

        else:
            col = X1
            col = np.asarray(col)
            X3 = np.hstack((X3, col))
        Y1 = X[k-2:k-1][X.columns[4:375]]
        Y1 = Y1.to_numpy()
        Y2=Y1
        if Y3 is None:
            Y3 = Y1
            Y3 = np.asarray(Y1)
        else:
            col1 = Y1
            col1 = np.asarray(col1)
            Y3 = np.hstack((Y3, col1))
    print(X3.shape)
    print(Y1.shape)
    c="csv"+str(l)
    with open(c, 'wb') as f:
        pickle.dump(X3, f)
    d="AQI"+str(l)
    print(c+" is ok")
    with open(d, 'wb') as f:
        pickle.dump(Y3, f)
    l+=1
    X3=None
    Y3 = None
    print(d + " is ok")
print("Y_AQI and csv is ok!")